package com.czwj.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.czwj.annotation.SystemControllerLog;
import com.czwj.entities.Company;
import com.czwj.entities.DataGridData;
import com.czwj.entities.OperateResult;
import com.czwj.entities.PageBean;
import com.czwj.entities.UserInfo;
import com.czwj.service.CompanyService;
import com.czwj.service.DepartmentService;
import com.czwj.service.LevelService;
import com.czwj.util.TextUtil;

import ch.qos.logback.classic.Level;

@Controller
public class CompanyAction{
	@Resource
	CompanyService companyService;
	
	@Resource
	DepartmentService departmentService;
	
	@Resource
	LevelService levelService;
	
	@RequestMapping(value = "/company",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<Company> company(HttpServletResponse response,
			@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows){
		DataGridData<Company> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		if(userInfo.getId() == 1){
		}else{
			map.put("id",userInfo.getCompanyid());
		}
		List<Company> list = companyService.search(map);
		result.setRows(list);
		result.setTotal(companyService.count(map));
		return result;
	}
	
	@RequestMapping(value = "/companylist",method = RequestMethod.POST)
	@ResponseBody
	public List<Company> companylist(){
		List<Company> list = companyService.search(null);
		return list;
	}
	
	@SystemControllerLog(description = "修改公司信息")
	@RequestMapping(value = "/companySave.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult companySave(@RequestBody Company company,HttpServletRequest request) throws Exception{
		OperateResult result = new OperateResult();
		int count = 0;
		if(company.getId() != null && company.getId() > 0){
			count = companyService.update(company);
		}else{
			Subject currentUser = SecurityUtils.getSubject();
			company.setCreaterid(((UserInfo)currentUser.getSession().getAttribute("userInfo")).getId());
			company.setCreatedate(new Date());
			company.setCreateip(request.getRemoteAddr());
			company.setStatus(2);
			count = companyService.insert(company);
			com.czwj.entities.Level level = new com.czwj.entities.Level();
			level.setName("管理员");
			level.setCompanyid(company.getId());
			levelService.insert(level);
		}
		result.setSuccess(count > 0);
		return result;
	}
	
	@SystemControllerLog(description = "禁用公司信息")
	@RequestMapping(value = "companyDelete.do",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult deleteCompany(@RequestParam("companyid") int id,@RequestParam("status") int status){
		OperateResult result = new OperateResult();
		try{
			Map<String,Object> map = new HashMap<>();
			map.put("id",id);
			map.put("status",status);
			int count = companyService.updateStatus(map);
			result.setSuccess(count > 0);
			return result;
		}
		catch(Exception e){
			// TODO Auto-generated catch block
			result.setSuccess(false);
			return result;
		}
	}
	
	@RequestMapping(value = "/check/companyname",method = RequestMethod.POST)
	@ResponseBody
	public OperateResult checkCompanyName(@RequestParam(value = "name",required = true) String name,
			@RequestParam(value = "id",required = true) int id){
		OperateResult result = new OperateResult();
		Company company = companyService.findByName(name);
		if(company == null){
			result.setSuccess(true);
		}else{
			if(company.getId() == id){
				result.setSuccess(true);
			}else{
				result.setSuccess(false);
			}
		}
		return result;
	}
}
